﻿using System;
using System.Collections.Generic;

using XStudio.Core.Data;
using XStudio.Core.Log;

using Core.Manage.User;

namespace Core.Manage.User
{
    /// <summary>
    /// 帐号表规则
    /// </summary>
    public partial class AccountRule : RuleObject<AccountEntity, AccountRule>
    {
        #region 新增帐号表规则
        /// <summary>
        /// 新增帐号表规则
        /// </summary>
        /// <param name="accountTypeID">0-农户，1-商户，2-生资，3-合作社</param>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <param name="createDateTime"></param>
        /// <returns>是否新增成功</returns>
        public Result Insert
            (
                int accountTypeID,
                string account,
                string password
            )
        {
            //实体信息
            var curEntity = new AccountEntity
            {
                UID = _Db.GetMaxID<AccountEntity>(),
                AccountTypeID = accountTypeID,
                Account = account,
                Password = password,
                CreateDateTime = DateTime.Now,
            };

            //执行操作并返回
            return Insert(curEntity);
        }

        /// <summary>
        /// 新增帐号表规则
        /// </summary>
        /// <param name="curEntity"></param>
        /// <returns>是否新增成功</returns>
        public new Result Insert(AccountEntity curEntity)
        {
            // 获取唯一标识
            //if(curEntity.UID == 0)
            //{
            //curEntity.UID = _Db.GetMaxID<AcountEntity>();
            //}

            // 返回结果对象
            var result = new Result();

            // 判断唯一性
            if (GetCount(t => t.Account == curEntity.Account) > 0)
            {
                result.IsOK = false;
                result.Description = "已有相同的帐号表，请检查！";
                return result;
            }

            //执行操作并返回
            try
            {
                result.IsOK = _Db.Insert(curEntity);
                result.StateCodeID = result.IsOK ? curEntity.UID : 0;
            }
            catch (Exception ex)
            {
                result.IsOK = false;
                LoggerHelper.WriteError("添加帐号表出现异常！", ex);
            }
            result.Description = string.Format("添加帐号表{0}", (result.IsOK ? "成功。" : "失败！"));
            return result;
        }
        #endregion

        #region 修改帐号表规则
        /// <summary>
        /// 修改帐号表规则
        /// </summary>
        /// <param name="accountID"></param>
        /// <param name="accountTypeID">0-农户，1-商户，2-生资，3-合作社</param>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <param name="createDateTime"></param>
        /// <returns>是否修改成功</returns>
        public Result Update
            (
                int accountID,
                string password
            )
        {
            //实体信息
            var curEntity = new AccountEntity
            {
                Password = password
            };

            //执行操作并返回
            return Update(curEntity, t => t.UID == accountID);
        }

        /// <summary>
        /// 修改帐号表规则
        /// </summary>
        /// <param name="curEntity"></param>
        /// <returns>是否新增成功</returns>
        public new Result Update(AccountEntity curEntity)
        {
            // 返回结果对象
            var result = new Result();

            //// 判断唯一性
            //if (GetCount(t => t.Model == curEntity.Model && t.UID != curEntity.UID) > 0)
            //{
            //    result.IsOK = false;
            //    result.Description = "已有相同的帐号表，请检查！";
            //    return result;
            //}

            //执行操作并返回
            try
            {
                result.IsOK = _Db.Update(curEntity, p => p.UID == curEntity.UID);
            }
            catch (Exception ex)
            {
                result.IsOK = false;
                LoggerHelper.WriteError("修改帐号表出现异常！", ex);
            }
            result.Description = string.Format("修改帐号表{0}", (result.IsOK ? "成功。" : "失败！"));
            return result;
        }
        #endregion

        #region 删除帐号表规则
        /// <summary>
        /// 删除帐号表规则
        /// </summary>
        /// <param name="uid">唯一标识</param>
        /// <returns>是否修改成功</returns>
        public new Result Delete(int uid)
        {
            //执行操作并返回
            var result = new Result();
            result.IsOK = _Db.Delete<AccountEntity>(p => p.UID == uid);
            result.Description = string.Format("删除帐号表{0}", (result.IsOK ? "成功。" : "失败！"));
            return result;
        }
        #endregion
    }
}
